import { useEffect, useState } from 'react';
import { Button, message } from 'antd';
import IconFont from '@/components/IconFont';
import TableRender from '../component/TableRender';
import SetTableColumns from '@/components/SetTableColumns';
import PrintModal from '../component/PrintModal';
import { queryTableList, savePrintData } from '../service';
import { pageCode } from '../config';
import { getColumnsData } from '../config/columns.jsx';


const StockSummary = (props) => {

  const { formData, initColumns, headerHeight, realColumns, updatePageConfig } = props;

  const [data, setData] = useState([]);

  const [visible, setVisible] = useState();

  useEffect(() => {
    getTableList();
  }, [formData]);

  //获取数据
  const getTableList = async () => {
    START_LOADING();
    const params = {
      supplierAreaList: formData.supplierAreaList?.length > 0 ? formData.supplierAreaList : undefined,
      supplierIdList: formData.supplierIdList?.length > 0 ? formData.supplierIdList : undefined
    }
    const { success, data } = await queryTableList(params);
    if (success) {
      setData(data);
    }
    END_LOADING();
  }

  //点击打印
  const handlePrint = () => {
    if(data?.pickingGoodsSummarySupplierVOList?.length > 0) {
      setVisible('printModal');
    } else {
      message.info('没有可打印的数据');
    }
  }

  //处理打印数据
  const getPrintData = (printName) => {
    const list = [];
    data.pickingGoodsSummarySupplierVOList.forEach((item) => {
      list.push({
        supplierName: item.supplierName,
        supplierArea: item.supplierArea,
        accountNo: item.accountNo,
        supplierSkuQuantity: item.supplierSkuQuantity,
        supplierSkuTotalPrice: item.supplierSkuTotalPrice,
        goodsList: item.pickingGoodsSummarySpuVOList,
        columns: getColumnsData({
          columns: realColumns,
        }),
        index: printName,
      });
    });
    return list;
  }

  //选择打印机
  const PrintModalProps = {
    getPrintData,
    handleOk() {
      savePrintData(data);
    },
    handleCancel() {
      setVisible(null);
    },
  };

  //列设置props
  const SetTableColumnsProps = {
    pageCode,
    initColumns,
    realColumns,
    updatePageConfig,
    handleCancel() {
      setVisible(null);
    },
    handleOk() {
      setVisible(null);
    },
  }

  const TableRenderProps = {
    headerHeight,
    data: data.pickingGoodsSummarySupplierVOList,
    columns: realColumns,
    updatePageConfig
  }

  return (
    <div style={{ background: '#fff', padding: '16px', marginTop: '16px' }}>
      <div style={{ display: 'flex', justifyContent: 'space-between' }}>
        <Button onClick={handlePrint}>打印备货汇总</Button>
        <div>
          <span style={{ marginRight: '40px' }}>数据生成时间: {data.pickingCompleteTime}</span>
          <Button type="text" onClick={() => setVisible('setColumns')}>
            <IconFont type="icona-lujing12" />
            列表设置
          </Button>
        </div>
      </div>
      <TableRender {...TableRenderProps} />
      {visible == 'setColumns' && <SetTableColumns {...SetTableColumnsProps} />}
      {visible == 'printModal' && <PrintModal {...PrintModalProps} />}
    </div>
  );
};
export default StockSummary;